package com.taru.order.controller;

import com.taru.order.common.JsonResult;
import com.taru.order.entity.Answer;
import com.taru.order.entity.Problem;
import com.taru.order.service.AnswerServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class AnswerController {

    AnswerServiceImpl answerService=new AnswerServiceImpl();

    /**
     * 添加答案
     */
    @RequestMapping(value = "/answers",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addAnswer(@RequestBody Answer answer){
        JsonResult result=null;
        try{
            int code=answerService.addAnswer(answer);
            if(code>0){
                result =new JsonResult(200,"添加成功");
            }else {
                result=new JsonResult(404,"添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 获取最新答案（列表）
     */
    @RequestMapping(value = "/answers",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult listAnswers(String problemId){
        JsonResult result=null;
        try{
            List list=answerService.listAnswers(problemId);
            if(list!=null){
                result =new JsonResult(200,"获取成功",list);
            }else {
                result=new JsonResult(404,"获取失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 修改答案
     */
    @RequestMapping(value = "/answers",method = RequestMethod.PUT)
    @ResponseBody
    public JsonResult modAnswer(@RequestBody Answer answer){
        JsonResult result=null;
        try{
            int code=answerService.modAnswer(answer);
            if(code>0){
                result =new JsonResult(200,"修改成功");
            }else {
                result=new JsonResult(404,"修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 删除答案
     */
    @RequestMapping(value = "/answers",method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult removeAnswes(@RequestBody List<Integer> list){
        JsonResult result=null;
        try{
            int code=answerService.deleteAnswers(list);
            if(code>0){
                result =new JsonResult(200,"删除成功");
            }else {
                result=new JsonResult(404,"删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
}
